# modules/views.py
from django.http import JsonResponse
from django.core.paginator import Paginator

from modules.models import Modules
from utils.common import result, removeEmpty


# 分页 + 模块名模糊查询
def get(request):
    page = request.GET.get("page", 1)
    size = request.GET.get("size", 10)
    name = request.GET.get("name", "")
    modules = Modules.objects.filter(is_delete=False, name__contains=name)
    moduless = Paginator(modules, size)
    modules_list = []
    for module in moduless.page(page):
        modules_list.append({
            'id': module.id,
            'name': module.name,
            'tester': module.tester,
            'project_id': module.project_id,
            'project_name': module.project_name,
            'desc': module.desc
        })
    result["data"] = {
        "list": modules_list,
        "pages": moduless.num_pages,
        "total": moduless.count
    }
    # JsonResponse第一个参数默认只能为dict字典，如果设为其他类型，需要将safe设为False
    return JsonResponse(result, safe=False, status=200)

# 创建数据
def post(request):
    try:
        json_data = request.body.decode('utf-8')
        if json_data:
            python_data = eval(json_data)
            Modules.objects.create(**python_data)
            result["message"] = "保存成功"
            result["success"] = True
            return JsonResponse(result, status=200)
    except Exception as e:
        result["message"] = e
        return JsonResponse(result,  safe=False, status=200)


# 更新数据
def put(request, pk):
    ret = Modules.objects.filter(id=pk, is_delete=False).values("id")
    if ret:
        json_data = request.body.decode('utf-8')
        if json_data:
            python_data = eval(json_data)
            a = Modules.objects.filter(name=python_data["name"], project_id=python_data["project_id"]).values("id").first()
            if not a or a["id"] == pk:
                d = removeEmpty(python_data)
                Modules.objects.filter(id=pk).update(**d)
                result["message"] = "操作成功"
                result["success"] = True
                result["data"] = d
                return JsonResponse(result, status=200)
            else:
                result["message"] = "该模块名已存在"
                return JsonResponse(result, status=200)
        return JsonResponse(result, status=200)
    else:
        result["message"] = "模块不存在"
        return JsonResponse(result, status=200)

# 逻辑删除
def delete(request, pk):
    ret = Modules.objects.filter(id=pk, is_delete=False).values("id").first()
    if ret:
        module = Modules.objects.get(id=pk)
        module.is_delete = True
        module.save()
        result["message"] = "删除成功"
        result["success"] = True
        return JsonResponse(result, status=200)
    result["message"] = "模块不存在"
    return JsonResponse(result, status=200)

# 获取id和name
def names(reuqest,project_id):
    modules = Modules.objects.filter(project_id=project_id, is_delete=False).values("id", "name")
    module_list = []
    for module in modules:
        module_list.append({
            'id': module["id"],
            'name': module["name"],
        })
    result["data"] = module_list
    return JsonResponse(result, safe=True, status=200)